package com.Team7.controller;

import com.Team7.call.AddFileCall;
import com.Team7.call.AddMenuCall;
import com.Team7.domain.Item;
import com.Team7.factory.DiskFactory;
import com.Team7.util.DiskUtil;
import com.Team7.util.FileUtil;
import com.Team7.util.StringUtil;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.awt.event.MouseEvent;
import java.io.File;
import java.util.zip.CheckedOutputStream;

public class FileController {
    private Stage stage;
    private AddFileCall addFileCall;
    private String curPath;

    @FXML
    private Button btn_createFile;

    @FXML
    private TextField tf_fileName;


    public FileController(AddFileCall addFileCall,String curPath){
        this.stage=new Stage();
        this.addFileCall=addFileCall;
        this.curPath=curPath;
        try{
            FXMLLoader fxmlLoader=new FXMLLoader(getClass().getResource("../fxml/AddFile.fxml"));
            fxmlLoader.setController(this);
            Scene scene=new Scene(fxmlLoader.load(),300,150) ;
            stage.setScene(scene);
            stage.setTitle("添加文件");
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    public void show(){
        this.stage.show();
    }


    @FXML
    private void initialize(){

        btn_createFile.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                String filename=tf_fileName.getText().trim();

                String absolatePath= StringUtil.getAbsolutePath(curPath,filename);
                Boolean success=FileUtil.createFile(DiskFactory.getDisk(),absolatePath,(byte) 4);
                //第一个初始化为 #
                //DiskFactory.getDisk()[bytes[6]][0]=35;
                if(success){
                    addFileCall.addFile(filename);
                    stage.close();
                }else{
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("Information Dialog");
                    alert.setHeaderText(null);
                    alert.setContentText("File has been created ,you have to change another name!");
                    alert.showAndWait();
                }
            }
        });

    }

}
